package cn.edu.klmy.dao.impl;

import cn.edu.klmy.dao.AbstractJdbcTemplate;
import cn.edu.klmy.dao.ICatalogDao;
import cn.edu.klmy.dao.IParamDao;
import cn.edu.klmy.models.menu.AuthorityCatalog;
import cn.edu.klmy.models.menu.AuthorityNode;
import cn.edu.klmy.models.menu.Catalog;
import cn.edu.klmy.models.menu.Node;
import cn.edu.klmy.models.param.SystemParam;
import com.googlecode.ehcache.annotations.Cacheable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 菜单DAO实现类
 *
 * @author 顾伟
 *         创建日期：13-4-6
 */
@Repository
public class CatalogDao extends AbstractJdbcTemplate implements ICatalogDao {

    /**
     * 通过角色获取功能目录树
     *
     * @param roleId 角色唯一标识
     * @return 功能目录树
     */
    @Override
//    @Cacheable(cacheName = "catalogCache")
    public List<Catalog> getCatalogByRole(int roleId,boolean isElective) {
        //根据权限取得目录
        String CATALOG_QUERY = "SELECT B.MENU_ID,B.MENU_NAME,B.MENU_TYPE,B.MENU_INDEX FROM ROLE_MENU A " +
                "LEFT JOIN MENU B ON A.MENU_ID=B.MENU_ID " +
                "WHERE A.ROLE_ID=? AND B.MENU_TYPE=2 ORDER BY B.MENU_INDEX";
        List<Catalog> catalogs = this.jdbcTemplate.query(CATALOG_QUERY, new Object[]{roleId}, new CatalogRowMapper());

        String NODE_QUERY = "SELECT B.MENU_ID,B.MENU_NAME,B.MENU_TYPE,B.MENU_INDEX,B.MENU_CODE,B.MENU_URL FROM ROLE_MENU A " +
                "LEFT JOIN MENU B ON A.MENU_ID=B.MENU_ID " +
                "WHERE A.ROLE_ID=? AND B.PARENT_MENU_ID=? AND B.MENU_TYPE=1";

        if(isElective){
            NODE_QUERY+=" AND B.MENU_NAME<>'排课管理'";
        }

        NODE_QUERY+=" ORDER BY B.MENU_INDEX";

        for (Catalog catalog : catalogs) {
            List<Node> nodes = this.jdbcTemplate.query(NODE_QUERY, new Object[]{roleId, catalog.getMenuId()}, new NodeRowMapper());
            catalog.setNodes(nodes);
        }

        return catalogs;
    }

    @Override
//    @Cacheable(cacheName = "catalogCache")
    public List<AuthorityCatalog> getRoleCatalog(int roleId) {
        String CATALOG_QUERY = "SELECT B.MENU_ID,B.MENU_NAME FROM ROLE_MENU A " +
                "LEFT JOIN MENU B ON A.MENU_ID=B.MENU_ID " +
                "WHERE A.ROLE_ID=? AND B.MENU_TYPE=2 ORDER BY B.MENU_INDEX";
        List<AuthorityCatalog> authorityCatalogs = this.jdbcTemplate.query(CATALOG_QUERY, new Object[]{roleId}, new AuthorityCatalogRowMapper());

        String NODE_QUERY = "SELECT B.MENU_ID,B.MENU_NAME FROM ROLE_MENU A " +
                "LEFT JOIN MENU B ON A.MENU_ID=B.MENU_ID " +
                "WHERE A.ROLE_ID=? AND B.PARENT_MENU_ID=? AND B.MENU_TYPE=1 ORDER BY B.MENU_INDEX";
        for (AuthorityCatalog authorityCatalog : authorityCatalogs) {
            List<AuthorityNode> authorityNodes = this.jdbcTemplate.query(NODE_QUERY, new Object[]{roleId, authorityCatalog.getId()}, new AuthorityNodeRowMapper());
            authorityCatalog.setChildren(authorityNodes);
        }

        return authorityCatalogs;
    }

    private class AuthorityNodeRowMapper implements RowMapper {

        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            AuthorityNode node = new AuthorityNode();
            node.setId(resultSet.getInt("MENU_ID"));
            node.setText(resultSet.getString("MENU_NAME"));
            return node;
        }
    }

    private class AuthorityCatalogRowMapper implements RowMapper {

        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            AuthorityCatalog catalog = new AuthorityCatalog();
            catalog.setId(resultSet.getInt("MENU_ID"));
            catalog.setText(resultSet.getString("MENU_NAME"));
            return catalog;
        }
    }

    /**
     * 节点映射
     */
    private class NodeRowMapper implements RowMapper {

        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            Node node = new Node();
            node.setMenuId(resultSet.getInt("MENU_ID"));
            node.setMenuName(resultSet.getString("MENU_NAME"));
            node.setMenuIndex(resultSet.getInt("MENU_INDEX"));
            node.setMenuType(resultSet.getInt("MENU_TYPE"));
            node.setMenuCode(resultSet.getString("MENU_CODE"));
            String url = resultSet.getString("MENU_URL");
            if (null == url || url.equals("")) {
                url = "temp";
            }
            node.setMenuUrl(url);
            return node;
        }
    }

    /**
     * 目录映射
     */
    private class CatalogRowMapper implements RowMapper {

        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            Catalog catalog = new Catalog();
            catalog.setMenuId(resultSet.getInt("MENU_ID"));
            catalog.setMenuName(resultSet.getString("MENU_NAME"));
            catalog.setMenuIndex(resultSet.getInt("MENU_INDEX"));
            catalog.setMenuType(resultSet.getInt("MENU_TYPE"));
            return catalog;
        }
    }
}
